package com.dobbinsoft.iotechn.apigw.ws.config;

import com.dobbinsoft.iotechn.apigw.ws.DataPointWebsocket;
import com.dobbinsoft.iotechn.apigw.ws.handler.WebsocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Description:
 * User: rize
 * Date: 2020/6/27
 * Time: 14:04
 */
@Configuration
public class WebSocketConfig {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    /**
     * 保存会话信息的 SESSION MAP
     * @return
     */
    @Bean
    public Map dataWsSessionMap() {
        return new ConcurrentHashMap<String, DataPointWebsocket>();
    }

    @Bean
    public Map<Integer, WebsocketHandler> wsHandlerMap(List<WebsocketHandler> list) {
        return list.stream().collect(Collectors.toMap(WebsocketHandler::getCode, item -> item));
    }

}
